********************************************************************************
** 	TITLE:		UK19_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       uk19                                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "UK19_results.smcl", replace

* Import the dataset

copy https://researchbriefings.files.parliament.uk/documents/CBP-8749/HoC-GE2019-results-by-candidate-csv.csv uk19.csv, replace
type uk19.csv
import delimited uk19.csv, clear

* Generate rank of parties

gsort constituency_name -votes
bysort constituency_name : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort constituency_name : gen winner19 = first[1]
bysort constituency_name : gen runnerup19 = second[2]
bysort constituency_name : gen thirdplace19 = third[3]

* Generate margins of victory

bysort constituency_name : gen margin = (share[1] - share[2])*100

* Generate effective number of electoral parties

gen sqrvotepr = (share)^2

bysort constituency_name : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Labelling parties

replace party_name = "1" if party_name == "Conservative"
replace party_name = "2" if party_name == "Labour"
replace party_name = "3" if party_name == "Liberal Democrat"
replace party_name = "4" if party_name == "Scottish National Party"
replace party_name = "5" if party_name == "Plaid Cymru"
replace party_name = "6" if party_name == "UK Independence Party"
replace party_name = "7" if party_name == "Green"
replace party_name = "8" if party_name == "British National Party"
replace party_name = "11" if party_name == "The Independent Group for Change"
replace party_name = "12" if party_name == "Brexit Party"
replace party_name = "13" if party_name == "Independent"
replace party_name = "9" if party_name!="1" & party_name!="2" & party_name!="3" & party_name!="4" & party_name!="5" & party_name!="6" & party_name!="7" & party_name!="11" & party_name!="12" & party_name!="13"

destring (party_name), replace

replace winner19 = "1" if winner19 == "Conservative"
replace winner19 = "2" if winner19 == "Labour"
replace winner19 = "3" if winner19 == "Liberal Democrat"
replace winner19 = "4" if winner19 == "Scottish National Party"
replace winner19 = "5" if winner19 == "Plaid Cymru"
replace winner19 = "6" if winner19 == "UK Independence Party"
replace winner19 = "7" if winner19 == "Green"
replace winner19 = "8" if winner19 == "British National Party"
replace winner19 = "11" if winner19 == "The Independent Group for Change"
replace winner19 = "12" if winner19 == "Brexit Party"
replace winner19 = "13" if winner19 == "Independent"
replace winner19 = "9" if winner19!="1" & winner19!="2" & winner19!="3" & winner19!="4" & winner19!="5" & winner19!="6" & winner19!="7" & winner19!="11" & winner19!="12" & winner19!="13"

destring (winner19), replace

replace runnerup19 = "1" if runnerup19 == "Conservative"
replace runnerup19 = "2" if runnerup19 == "Labour"
replace runnerup19 = "3" if runnerup19 == "Liberal Democrat"
replace runnerup19 = "4" if runnerup19 == "Scottish National Party"
replace runnerup19 = "5" if runnerup19 == "Plaid Cymru"
replace runnerup19 = "6" if runnerup19 == "UK Independence Party"
replace runnerup19 = "7" if runnerup19 == "Green"
replace runnerup19 = "8" if runnerup19 == "British National Party"
replace runnerup19 = "11" if runnerup19 == "The Independent Group for Change"
replace runnerup19 = "12" if runnerup19 == "Brexit Party"
replace runnerup19 = "13" if runnerup19 == "Independent"
replace runnerup19 = "9" if runnerup19!="1" & runnerup19!="2" & runnerup19!="3" & runnerup19!="4" & runnerup19!="5" & runnerup19!="6" & runnerup19!="7" & runnerup19!="11" & runnerup19!="12" & runnerup19!="13"

destring (runnerup19), replace

replace thirdplace19 = "1" if thirdplace19 == "Conservative"
replace thirdplace19 = "2" if thirdplace19 == "Labour"
replace thirdplace19 = "3" if thirdplace19 == "Liberal Democrat"
replace thirdplace19 = "4" if thirdplace19 == "Scottish National Party"
replace thirdplace19 = "5" if thirdplace19 == "Plaid Cymru"
replace thirdplace19 = "6" if thirdplace19 == "UK Independence Party"
replace thirdplace19 = "7" if thirdplace19 == "Green"
replace thirdplace19 = "8" if thirdplace19 == "British National Party"
replace thirdplace19 = "11" if thirdplace19 == "The Independent Group for Change"
replace thirdplace19 = "12" if thirdplace19 == "Brexit Party"
replace thirdplace19 = "13" if thirdplace19 == "Independent"
replace thirdplace19 = "9" if thirdplace19!="1" & thirdplace19!="2" & thirdplace19!="3" & thirdplace19!="4" & thirdplace19!="5" & thirdplace19!="6" & thirdplace19!="7" & thirdplace19!="11" & thirdplace19!="12" & thirdplace19!="13"

destring (thirdplace19), replace

label define party 1 "Conservative" 2 "Labour" 3 "Liberal Democrat" 4 "SNP" 5 "Plaid Cymru" 6 "UKIP" 7 "Green" 8 "BNP" 9 "Other" 11 "Change UK – The Independent Group" 12 "Brexit Party" 13 "Independent"
label values party_name winner19 runnerup19 thirdplace19 party
 
* Selecting, ordering, and sorting variables

keep ons_id constituency_name party_name rank votes share margin enep winner19 runnerup19 thirdplace19

order ons_id constituency_name party_name rank votes share margin enep winner19 runnerup19 thirdplace19

sort ons_id rank

drop if rank > 3

* Drop duplicates

duplicates tag ons_id, gen(dup)
duplicates drop ons_id, force
drop dup

* Change constituency ID

replace ons_id = subinstr(ons_id,"E1400","1",.)
replace ons_id = subinstr(ons_id,"S1400","2",.)
replace ons_id = subinstr(ons_id,"W0700","3",.)
replace ons_id = subinstr(ons_id,"N0600","4",.)

* Create election variable

gen election19 = "2019 British general election"

* Saving file

save UK19_results, replace

* Merge with UK17_results.dta and generate reelection variable

merge 1:1 _n using UK17_results.dta

* Generate variable for incumbent party reelection

gen reelected19 = 1 if winner19 == winner17
replace reelected19 = 0 if reelected19!=1

* Saving file

save UK1719_results, replace

log close